package com.lupan.effectiveJava.chapter2_initAndDestroyObject;

import java.util.Calendar;
import java.util.Date;

/**
 * 说明：尽量重用不可变得对象
 *
 * @author lupan
 * @version 2016/5/27 15:12
 */
public class Person {

    /**
     * 无效率的方法
     * @param birthDate
     * @return
     */
    public boolean isBabyBoomer(Date birthDate){
        Calendar calendar = Calendar.getInstance();
        calendar.set(1946,Calendar.JANUARY,1,0,0,0);
        Date boomStart = calendar.getTime();
        calendar.set(1965,Calendar.JANUARY,1,0,0,0);
        Date boomEnd = calendar.getTime();
        return birthDate.compareTo(boomStart)>=0&&birthDate.compareTo(boomEnd)<0;
    }


    /**
     * 更有效率的方法
     */
    private static final Date BOOM_START;
    private static final Date BOOM_END;

    static{
        Calendar calendar = Calendar.getInstance();
        calendar.set(1946,Calendar.JANUARY,1,0,0,0);
        BOOM_START = calendar.getTime();
        calendar.set(1965,Calendar.JANUARY,1,0,0,0);
        BOOM_END = calendar.getTime();
    }

    public boolean isBabyBoomerEffective(Date birthDate){
        return birthDate.compareTo(BOOM_START)>=0&&birthDate.compareTo(BOOM_END)<0;
    }
}
